import wxValidate from 'wechat-validate'
Page({
  behaviors:[wxValidate],
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    appointment:'',
    houseId:'',
    mobile:'',
    description:'',
    houseInfo:'',
    houseList: [],
    repairItem: [],
    repairItemId:'',
    repairItemName:'',
    attachment: [
    ],
  },
  //校验
  rules:{
    houseId:[{required: true, message: '请填写报修房屋!'}],
    repairItemId:[{required: true, message: '请填写维修项目!'}],
    mobile:[{required: true, message: '请填写手机号码!'},
    {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},],
    appointment:[{required: true, message: '请填写预约日期!'}],
    description:[{required: true, message: '请填写问题描述!'}]
  },
  onLoad({id}){
    this.getHouseList()
    this.getrepairItem()
    wx.setNavigationBarTitle({
      title: id?'编辑维修房屋信息':'填写维修房屋'
    })
   if(id)  this.getHouseListTwo(id)
  },
  //编辑项目，判断是否有id 
  async getHouseListTwo(id){
    const {code,data} = await wx.http.get('/repair/'+id)
    if(code!==10000) return wx.utils.toast()
    this.setData({...data})
  },
  //获取报修房屋信息 
  async getHouseList(){
    const {code, data:houseList} = await wx.http.get('/house')
    if(code!==10000) return wx.utils.toast()
    this.setData({houseList})
  },
  //报修房屋点击具体项触发的事件
  selectHouse(ev){
    // console.log(ev);
    this.setData({
      houseId:ev.detail.id,
      houseInfo:ev.detail.name
    })
  },
  //维修项目
  async getrepairItem(){
    const {code ,data:repairItem} = await wx.http.get('/repairItem')
    if(code!==10000) return wx.utils.toast()
    this.setData({repairItem})
  },
  //选择具体项目触发的事件
  selectRepairLayer(ev){
    this.setData({
      repairItemId:ev.detail.id,
      repairItemName:ev.detail.name
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
//点击确定时间
getDate(ev){
  console.log(ev);
  this.setData({ dateLayerVisible: false ,
    appointment:wx.utils.dataFormat(ev.detail)
  })
},
//选择照片板块板块
getAttachment(ev){
  // console.log(ev.detail.file.url);
  wx.uploadFile({
    filePath: ev.detail.file.url,
    name: 'file',
    url: wx.http.baseURL+'/upload',
    header:{Authorization:'Bearer '+getApp().token} ,
    success:({data:res})=>{
      const data = JSON.parse(res)
      if(data.code!==10000) return wx.utils.toast()
      const attachment = this.data.attachment
      attachment.push(data.data)
      this.setData({
        attachment
      })
    }
  })
},
//删除照片模块
delAttachment(ev){
   this.setData({
    attachment:this.data.attachment.filter(item=>item.id!==ev.detail.file.id)
   })
},
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  //提交
  async submitForm() {
    if(!this.validate()) return
    const {houseId,repairItemId,mobile,appointment,description,attachment} = this.data
    const {code} = await wx.http.post('/repair',{houseId,repairItemId,mobile,appointment,description,attachment})
    // console.log(code);
    if(code!==10000) return wx.utils.toast('报修失败')
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
